package fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.travel.R;
import com.travel.photos.FragmentAdapter;
import com.travel.photos.PhotoFocusFragment;
import com.travel.photos.ui.find.PhotoVpFindFragment;
import com.travel.photos.ui.travel.PhotoVpTravelFragment;
import com.travel.photos.util.HideService;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/17.
 */
public class PhotosFragment extends Fragment {
    @Bind(R.id.photo_tablayout)
    TabLayout tablayout;
    @Bind(R.id.photo_image)
    ImageView image;
    @Bind(R.id.photo_vp)
    ViewPager viewpager;
    private View mView;
    private List<Fragment> mList = new ArrayList<>();
    private List<String> titlelist = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startHideService();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_photo, container, false);
        ButterKnife.bind(this, mView);
        initdata();
        initview();
        return mView;
    }

    private void initdata() {
        PhotoVpFindFragment findFragment = new PhotoVpFindFragment();
        PhotoFocusFragment focusFragment = new PhotoFocusFragment();
        PhotoVpTravelFragment travelFragment = new PhotoVpTravelFragment();
        mList.add(findFragment);
        mList.add(focusFragment);
        mList.add(travelFragment);
        titlelist.add("发现");
        titlelist.add("关注");
        titlelist.add("轻游记");
    }
    /**
     * 开始预加载进程
     */
    private void startHideService(){
        Intent intent = new Intent(getActivity(), HideService.class);
        getActivity().startService(intent);
    }

    private void stopHideService(){
        Intent intent = new Intent(getActivity(), HideService.class);
        getActivity().stopService(intent);
    }

    private void initview() {
        FragmentAdapter adapter = new FragmentAdapter(getActivity().getSupportFragmentManager(),mList,titlelist);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(3);
        tablayout.setupWithViewPager(viewpager);
        tablayout.setTabGravity(TabLayout.GRAVITY_CENTER); //tab居中显示
        //tab的字体选择器,默认黑色,选择时红色
        tablayout.setTabTextColors(Color.BLACK, Color.RED);
        //tab的下划线颜色,默认是粉红色
        tablayout.setSelectedTabIndicatorColor(Color.RED);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        stopHideService();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.photo_image)
    public void onClick() {
    }
}
